package com.tengfei.api.exam;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tengfei.common.annotation.Log;
import com.tengfei.common.annotation.RepeatSubmit;
import com.tengfei.common.core.controller.BaseController;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.core.validate.AddGroup;
import com.tengfei.common.core.validate.EditGroup;
import com.tengfei.common.enums.BusinessType;
import com.tengfei.main.exam.domain.bo.PaperBo;
import com.tengfei.main.exam.domain.bo.PaperQuQueryBo;
import com.tengfei.main.exam.domain.bo.PaperUserBo;
import com.tengfei.main.exam.domain.bo.PaperQuBo;
import com.tengfei.main.exam.domain.vo.PaperUserVo;
import com.tengfei.main.exam.domain.vo.PaperVo;
import com.tengfei.main.exam.service.IPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 试卷Controller
 *
 * @author tengfei
 * @date 2022-07-31
 */
@Validated
@Api(value = "试卷控制器", tags = {"试卷管理"})
@RestController
@RequestMapping("/exam/paper")
public class PaperController extends BaseController {

    @Resource
    private IPaperService iPaperService;

    /**
     * 查询试卷列表
     */
    @ApiOperation("查询试卷列表")
    @SaCheckPermission("exam:paper:list")
    @GetMapping("/list")
    public TableDataInfo<PaperVo> list(PaperBo bo, PageQuery pageQuery) {
        return iPaperService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取试卷详细信息
     */
    @ApiOperation("获取试卷详细信息")
    @SaCheckPermission("exam:paper:query")
    @GetMapping("/{paperId}")
    public R<PaperVo> getInfo(@ApiParam("试卷id")
                              @NotNull(message = "试卷id不能为空")
                              @PathVariable("paperId") Long paperId) {
        return R.ok(iPaperService.queryById(paperId));
    }

    /**
     * 新增试卷
     */
    @ApiOperation("新增试卷")
    @SaCheckPermission("exam:paper:add")
    @Log(title = "新增试卷", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PaperBo bo) {
        return toAjax(iPaperService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改试卷
     */
    @ApiOperation("修改试卷")
    @SaCheckPermission("exam:paper:edit")
    @Log(title = "修改试卷", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PaperBo bo) {
        return toAjax(iPaperService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 批量删除试卷
     */
    @ApiOperation("批量删除试卷")
    @SaCheckPermission("exam:paper:remove")
    @Log(title = "批量删除试卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    public R<Void> remove(@ApiParam("试卷id数组")
                          @NotEmpty(message = "试卷id数组不能为空")
                          @RequestParam("examIds") List<Long> examIds) {
        return toAjax(iPaperService.deleteWithValidByIds(examIds) ? 1 : 0);
    }


    /**
     * 查询试卷已绑定题目列表
     */
    @ApiOperation("查询试卷已绑定题目列表")
    @SaCheckPermission("exam:paper:list")
    @GetMapping("/list/qu/bind")
    public TableDataInfo bindQuList(PaperQuQueryBo bo, PageQuery pageQuery) {
        return iPaperService.bindQuList(bo, pageQuery);
    }

    /**
     * 查询试卷未绑定题目列表
     */
    @ApiOperation("查询试卷未绑定题目列表")
    @SaCheckPermission("exam:paper:list")
    @GetMapping("/list/qu/unBind")
    public TableDataInfo unBindQuList(PaperQuQueryBo bo, PageQuery pageQuery) {
        return iPaperService.unBindQuList(bo, pageQuery);
    }

    /**
     * 修改题目分数
     */
    @ApiOperation("修改题目分数")
    @SaCheckPermission("exam:paper:edit")
    @Log(title = "修改题目分数", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/qu/editScore")
    public R<Void> editQuScore(@RequestBody PaperQuBo bo) {
        return toAjax(iPaperService.updateQuScore(bo) ? 1 : 0);
    }


    /**
     * 试卷绑定题目
     */
    @ApiOperation("试卷绑定题目")
    @SaCheckPermission("exam:paper:add")
    @Log(title = "试卷绑定题目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/qu")
    public R<Void> addQu(@RequestBody List<PaperQuBo> bos) {
        return toAjax(iPaperService.addQuByQuBo(bos) ? 1 : 0);
    }

    /**
     * 试卷解绑题目
     */
    @ApiOperation("试卷解绑题目")
    @SaCheckPermission("exam:paper:remove")
    @Log(title = "试卷解绑题目", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @DeleteMapping("/qu")
    public R<Void> removeQu(@RequestBody List<PaperQuBo> bos) {
        return toAjax(iPaperService.removeQuByQuBo(bos) ? 1 : 0);
    }

    /**
     * 查询试卷已绑定用户列表
     */
    @ApiOperation("查询试卷已绑定用户列表")
    @SaCheckPermission("exam:paper:list")
    @GetMapping("/list/user/bind")
    public TableDataInfo<PaperUserVo> bindUserList(PaperUserBo bo, PageQuery pageQuery) {
        return iPaperService.bindUserList(bo, pageQuery);
    }

    /**
     * 查询试卷未绑定用户列表
     */
    @ApiOperation("查询试卷未绑定用户列表")
    @SaCheckPermission("exam:paper:list")
    @GetMapping("/list/user/unBind")
    public TableDataInfo<PaperUserVo> unBindUserList(PaperUserBo bo, PageQuery pageQuery) {
        return iPaperService.unBindUserList(bo, pageQuery);
    }

    /**
     * 试卷批量绑定用户
     */
    @ApiOperation("试卷批量绑定用户")
    @SaCheckPermission("exam:paper:add")
    @Log(title = "试卷批量绑定用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/user")
    public R<Void> addUser(@RequestBody List<PaperUserBo> bos) {
        return toAjax(iPaperService.insertBatchUserByBo(bos) ? 1 : 0);
    }

    /**
     * 试卷批量解绑用户
     */
    @ApiOperation("试卷批量解绑用户")
    @SaCheckPermission("exam:paper:remove")
    @Log(title = "试卷批量解绑用户", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @DeleteMapping("/user")
    public R<Void> delUser(@RequestBody List<PaperUserBo> bos) {
        return toAjax(iPaperService.deleteBatchUserByBo(bos) ? 1 : 0);
    }
}
